﻿using SIG.Model.Identity;
using SIG.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SIG.WebMVC.Controllers
{
    public abstract class BaseController : Controller
    {
       public AjaxResultVM AR = new AjaxResultVM();

        //protected override void Initialize(System.Web.Routing.RequestContext requestContext)
        //{
            //int UrlId = getCurrenPageId();
            ////check if the user opening the site for the first time 
            //if (requestContext.HttpContext.Session["URLHistory"] != null)
            //{
            //    //The session variable exists. So the user has already visited this site and sessions is still alive. Check if this page is already visited by the user
            //    List<int> HistoryURLs = (List<int>)requestContext.HttpContext.Session["URLHistory"];
            //    if (HistoryURLs.Exists((element => element == UrlId)))
            //    {
            //        //If the user has already visited this page in this session, then we can ignore this visit. No need to update the counter.
            //        requestContext.HttpContext.Session["VisitedURL"] = 0;
            //    }
            //    else
            //    {
            //        //if the user is visting this page for the first time in this session, then count this visit and also add this page to the list of visited pages(URLHistory variable)
            //        HistoryURLs.Add(UrlId);
            //        requestContext.HttpContext.Session["URLHistory"] = HistoryURLs;

            //        //Make a note of the page Id to update the database later 
            //        requestContext.HttpContext.Session["VisitedURL"] = UrlId;
            //    }
            //}
            //else
            //{
            //    //if there is no session variable already created, then the user is visiting this page for the first time in this session. Then create a session variable and take the count of the page Id
            //    List<int> HistoryURLs = new List<int>();
            //    HistoryURLs.Add(UrlId);
            //    requestContext.HttpContext.Session["URLHistory"] = HistoryURLs;
            //    requestContext.HttpContext.Session["VisitedURL"] = UrlId;
            //}

        //    base.Initialize(requestContext);
        //}
        protected virtual new CustomPrincipal User
        {
            get { return HttpContext.User as CustomPrincipal; }
        }

        protected string GetModelErrorMessage()
        {
            string validationErrors = string.Join("|",
                    ModelState.Values.Where(E => E.Errors.Count > 0)
                    .SelectMany(E => E.Errors)
                    .Select(E => E.ErrorMessage)
                    .ToArray());
            return validationErrors;
        }


        protected string RenderPartialViewToString()
        {
            return RenderPartialViewToString(null, null);
        }

        protected string RenderPartialViewToString(string viewName)
        {
            return RenderPartialViewToString(viewName, null);
        }

        protected string RenderPartialViewToString(object model)
        {
            return RenderPartialViewToString(null, model);
        }

        protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = ControllerContext.RouteData.GetRequiredString("action");

            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

                return sw.GetStringBuilder().ToString();
            }
        }
    }
}